const { cookieExpires } = '@/config'
import Cookies from 'js-cookie'
import config from '@/config'

export const TOKEN_KEY = 'token'

const { title } = config

// 设置token
export const setToken = (token) => {
  Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}

// 获取token
export const getToken = () => {
  const token = Cookies.get(TOKEN_KEY)
  if (token) return token
  else return false
}

/**
 * 虚拟滚动条的颜色和宽度
 */
export function getScroll () {
  return {
    bar: {
      background: '#888',
      keepShow: false,
      size: '5px',
      minSize: 0.2
    },
    scrollButton: {
      enable: true,
      background: '#fff'
    }
  }
}

/**
 * 加载组件
 * @param {} obj 
 */
export function lazyRouter (obj) { 
  let compontent = obj.compontent
  if (compontent) { 
    return () => import(`@/${compontent}.vue`)
  }
}

/**
 * 装载路由
 * @param {} routers 
 * @param {*} data 
 */
export function getRouter (routers, data) {
  generaMenu(routers, data)
}

function generaMenu (routers, data) {
  if (Array.isArray(data)) {
    data.forEach((item) => {
      let menu = Object.assign({}, item)
      if (menu.path) {
        menu.component = lazyRouter(menu)
        if (item.children && item.children.length !== 0) {
          menu.children = []
          generaMenu(menu.children, item.children, menu.path)
        }
        routers.push(menu)
      }
    })
  }
}

/**
 * 设置本地存储
 */
export const localSave = (key, value) => {
  localStorage.setItem(key, value)
}

/**
 * 设置标题
 */
export const setTitle = () => {
  return document.title = `${title}`
}


/**
 * 获得屏幕的高度
 */
export const getClientHeight = () => {
  return document.documentElement.clientHeight
}

export const getFileSize = (fileByte) => {
    var fileSizeByte = fileByte;
    var fileSizeMsg = "";
    if (fileSizeByte < 1048576) fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
    else if (fileSizeByte == 1048576) fileSizeMsg = "1MB";
    else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
    else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) fileSizeMsg = "1GB";
    else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    else fileSizeMsg = "文件超过1TB";
    return fileSizeMsg;
}